package dq.study.designmodel.principle.开闭原则;

/**
 * @author dongqun
 * @date 2020/9/16 11:13
 */
public class Phone {
    private Button[] digitButtons;
    private Button sendButton;
    private Dialer dialer;

    public Phone() {
        dialer = new Dialer();
        digitButtons = new Button[10];
        for (int i = 0; i < digitButtons.length; i++){
            digitButtons[i] = new Button();
            final int digit = i;
            digitButtons[i].addListener(new ButtonListener() {
                @Override
                public void pressButton() {
                    dialer.enterDigit(digit);
                }
            });
        }
        sendButton = new Button();
        sendButton.addListener(new ButtonListener() {
            @Override
            public void pressButton() {
                dialer.dial();
            }
        });
    }

    public static void main(String[] args){
        Phone phone = new Phone();
        phone.digitButtons[9].press();
        phone.digitButtons[1].press();
        phone.digitButtons[1].press();
        phone.sendButton.press();
    }
}
